package quik.coworker;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.QuikHashMap;
import comm.util.RequestManager;
import comm.util.SqlMapLoader;

public class Coworker {
	public Coworker(){}
	
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	
	public ArrayList getUserCoworkers(HttpServletRequest request) {
		ArrayList lt = null;	
		
		try {
			lt = (ArrayList)sqlMapper.queryForList("coworker.getUserCoworkers", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lt;
	}
	
	public ArrayList getUserFollwers(HttpServletRequest request) {
		ArrayList lt = null;	
		
		try {
			lt = (ArrayList)sqlMapper.queryForList("coworker.getUserFollwers", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lt;
	} 
	public ArrayList getTotalCoworkers(HttpServletRequest request) {
		ArrayList lt = null;	
		QuikHashMap hm = new QuikHashMap();
		hm.put("GROUP_SEQ", request.getSession().getAttribute("SESSION_MEMBER_GROUPSEQ").toString());
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		try {
			lt = (ArrayList)sqlMapper.queryForList("coworker.getTotalCoworkers",hm);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lt;
	} 
	
	public ArrayList getUserGroupCoworkers(HttpServletRequest request) {
		ArrayList lt = null;	
		
		try {
			lt = (ArrayList)sqlMapper.queryForList("coworker.getUserGroupCoworkers", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lt;
	} 
	
	public String delCoworker(HttpServletRequest request) throws UnsupportedEncodingException{
		String result = "FAIL";
		QuikHashMap hm = new QuikHashMap();
		hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		
		try {
			sqlMapper.delete("coworker.delCoworker",hm);
			result = "SUCCESS";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result;
		}
		
		return result;
	}
	
	public String regCoworker(HttpServletRequest request) throws UnsupportedEncodingException{
		String result = "FAIL";
		QuikHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
		try {
			if(sqlMapper.queryForObject("coworker.checkExistCoworker",hm).toString().equals("0")){
				sqlMapper.insert("coworker.regCoworker",hm);
				result = "SUCCESS";
			}else{
				result = "DUPLICATED";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result;
		}
		return result;
	}
	

}
